package com.gzhryc.shared_device.oem.h5.mng.controllers.agent;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.jdbc.DateBranchHelper;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentRelation;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentRelationWallet;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentRelationWalletOperate;
import com.gzhryc.shared_device.oem.code.agent.services.AgentRelationService;
import com.gzhryc.shared_device.oem.code.agent.services.AgentRelationWalletOperateService;
import com.gzhryc.shared_device.oem.code.agent.services.AgentRelationWalletService;
import com.gzhryc.shared_device.oem.code.agent.services.dto.AgentRelationWalletOperateSearch;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;
import com.gzhryc.system.SysConstants;
import com.gzhryc.system.dao.db.Manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

public class AgentRelationWalletController {

    @Route(value = {"/agent/relation/wallet/info.html"})
    public String info(HttpServletRequest request) {
        Long userId = WebUtils.getLong(request,"userId");
        Long relationUserId = WebUtils.getLong(request,"relationUserId");

        AgentRelation agentRelation = AgentRelationService.self().getJoinByKeys(userId,relationUserId);
        if (agentRelation != null) {
        	Long money = 0L;
        	Long withdrawnMoney = 0L;
        	
            WebUtils.setAttribute(request, agentRelation);
            List<AgentRelationWallet> walletList = AgentRelationWalletService.self().findJoinByUserKeys(agentRelation.getUserId(),agentRelation.getRelationUserId());
            if(walletList != null) {
                request.setAttribute("walletList", walletList);
                
                for(AgentRelationWallet wallet : walletList) {
                	money = money + wallet.getMoney();
                	withdrawnMoney = withdrawnMoney + wallet.getWithdrawnMoney();
                }
            }
            
            request.setAttribute("moneyStr", NumberTools.getMoneyLiStr(money));
            request.setAttribute("withdrawnMoneyStr", NumberTools.getMoneyLiStr(withdrawnMoney));
        }
        return "/agent/relation/wallet/info.html";
    }

    @Route(value = {"/agent/relation/wallet/withdrawal_money/transfer.html"})
    public String transfer(HttpServletRequest request) {
        Long userId = WebUtils.getLong(request, "userId");
        Long relationUserId = WebUtils.getLong(request,"relationUserId");
        Long accountGroupId = WebUtils.getLong(request,"accountGroupId");
        AgentRelationWallet wallet = AgentRelationWalletService.self().getJoinByKeys(userId,relationUserId,accountGroupId);
        WebUtils.setAttribute(request,wallet);
        return "/agent/relation/wallet/transfer.html";
    }

    @Route(value = {"/agent/relation/wallet/withdrawal_money/transfer.do"})
    public AjaxResult doTransfer(HttpServletRequest request, HttpSession session){
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"转出失败");
        Manager manager = WebUtils.getAttribute(session, SysConstants.SESSION_LOGIN_MANAGER, Manager.class);
        if (manager == null) {
            result.setCode(DtoConstants.LOGIN_OVERDUE, "登录过期，请重新登录");
            return result;
        }
        Long userId = WebUtils.getLong(request, "userId");
        Long relationUserId = WebUtils.getLong(request,"relationUserId");
        Long accountGroupId = WebUtils.getLong(request, "accountGroupId");
        Double money = WebUtils.getDouble(request,"money");
        String operateNote = WebUtils.getString(request,"operateNote");


        OperateInfo operateInfo = new OperateInfo(WebConstants.PROGRAM_TYPE,WebConstants.PROGRAM_TYPE_NAME);
        operateInfo.setOperator( EOperatorType.Manager.index(),manager.getFullName());
        operateInfo.setOperateNote(operateNote);

        try {
            if(AgentRelationWalletService.self().transferMoneyToWithdrawal(userId,relationUserId,accountGroupId,NumberTools.changeMoneyLi(money),operateInfo)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = {"/agent/relation/wallet/operate/list.html"})
    public String operateList(HttpServletRequest request) {
        Long userId = WebUtils.getLong(request, "userId");
        Long relationUserId = WebUtils.getLong(request,"relationUserId");
        Long accountGroupId = WebUtils.getLong(request,"accountGroupId");
        AgentRelationWallet wallet = AgentRelationWalletService.self().getJoinByKeys(userId,relationUserId,accountGroupId);
        WebUtils.setAttribute(request,wallet);
        
        request.setAttribute("dateBranchItemList", DateBranchHelper.self().getMonthDateBranchItems());
        return "/agent/relation/wallet/operate_record.html";
    }

    @Route(value = "/agent/relation/wallet/operate/list.data")
    public ListResult<AgentRelationWalletOperate> operateListDate(HttpServletRequest request) {
        ListResult<AgentRelationWalletOperate> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        AgentRelationWalletOperateSearch search = WebUtils.getParameter(request,AgentRelationWalletOperateSearch.class);
        if(search.getUserId() != null && search.getRelationUserId() != null && search.getAccountGroupId() != null) {
            Integer pageNo = WebUtils.getInteger(request, "page", 1);
            Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
            
            Date dateBranch = new Date();
            if(StringTools.isNotBlank(search.getDateBranchId())){
                dateBranch = DateBranchHelper.getDateByItemId(search.getDateBranchId());
            }

            result.getBody().setData(AgentRelationWalletOperateService.self(dateBranch).findBySearch(search,pageNo,pageLimit));
            if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        }
        return result;
    }
}
